package com.example.a2022application.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.a2022application.BaseActivity;
import com.example.a2022application.R;
import com.example.a2022application.bean.GroupMessageBean;
import com.example.a2022application.net.Urls;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;

public class DiscussActivity extends BaseActivity implements View.OnClickListener {
    private TextView tv_title;
    private ImageView img_add;
    private ListView listdiscuss;
    String groupName;
    List<GroupMessageBean.DataBean> tList= new ArrayList();
    private ShowListAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discuss);
        tv_title = findViewById(R.id.tv_title);
        img_add = findViewById(R.id.img_add);
        listdiscuss = findViewById(R.id.listdiscuss);
        img_add.setOnClickListener(this);
        Intent intent = getIntent();
        groupName = intent.getStringExtra("groupName");
        tv_title.setText(groupName+"留言");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.img_add:
                Intent intent = new Intent(DiscussActivity.this,AddMessageActivity.class);
                intent.putExtra("gname",groupName);
                startActivity(intent);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        OkGo.<String>post(Urls.SELECTGROUPMESSAGE)
                .params("groupName",groupName)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        GroupMessageBean bean = getGson().fromJson(response.body(),GroupMessageBean.class);
                        if (bean.getCode() == 0){
                            tList = bean.getData();
                            adapter = new ShowListAdapter();
                            listdiscuss.setAdapter(adapter);
                        }
                    }
                });

    }

    public class ShowListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return tList.size();
        }

        @Override
        public Object getItem(int i) {
            return tList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ShowListAdapter.ViewHolder viewHolder;
            //判断界面是否有缓存
            if(convertView == null){
                viewHolder= new ShowListAdapter.ViewHolder();
                convertView= LinearLayout.inflate(parent.getContext(), R.layout.item_groupmsg, null);

                viewHolder.tv_msg = convertView.findViewById(R.id.tv_msg);
                viewHolder.tv_time = convertView.findViewById(R.id.tv_time);

                convertView.setTag(viewHolder);
            }else{
                viewHolder=(ShowListAdapter.ViewHolder)convertView.getTag();
            }
            GroupMessageBean.DataBean group = (GroupMessageBean.DataBean) tList.get(position);

            viewHolder.tv_msg.setText(group.getGmessage());
            viewHolder.tv_time.setText(group.getGmessageusername()+"  "+group.getCreate_time());

            return convertView;
        }

        private class ViewHolder{
            TextView tv_msg;
            TextView tv_time;

        }
    }
}
